package net.kleinhenz.questor.ui;

import net.kleinhenz.questor.Questor;
import net.kleinhenz.questor.R;
import net.kleinhenz.questor.game.OnChoiceListener;
import net.kleinhenz.questor.model.Choice;
import net.kleinhenz.questor.model.Content;
import net.kleinhenz.questor.model.Station;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;

import com.google.android.maps.MapView;

/**
 * Implements the main story view. This should be like an ebook reader with
 * added choice options, images, sound, styles etc.
 * 
 * From this view, you can access the player data, save and load screens and
 * settings or skip back to the main screen.
 * 
 * @author Michael Kleinhenz, m.kleinhenz@tarent.de
 */
public class StoryActivity extends Activity implements OnChoiceListener 

{
	private WebStoryView webView = null;
	private WebPlayerView playerDrawer = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.storyview);

		// webView = (WebStoryView) findViewById(R.id.storyWeb);
		// webView.addOnChoiceListener(this);

		LayoutInflater inflater = getLayoutInflater();
		View view = inflater.inflate(R.layout.playerdrawer, null);
		getWindow().addContentView(
				view,
				new LayoutParams(LayoutParams.FILL_PARENT,
						LayoutParams.FILL_PARENT));

		playerDrawer = (WebPlayerView) findViewById(R.id.playerDrawer);
		playerDrawer.reloadPlayerData();

		Questor.getCurrentGame().setCurrentStoryActivity(this);
		goTo(Questor.getCurrentGame().getCurrentQuest().getRoot());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		MenuItem itemSave = menu.add(0, 0, 1, "Save");
		itemSave.setIcon(android.R.drawable.ic_menu_save);
		MenuItem itemLoad = menu.add(0, 0, 2, "Load");
		itemLoad.setIcon(android.R.drawable.ic_menu_revert);

		MenuItem itemSettings = menu.add(0, 1, 3, "Preferences");
		itemSettings.setIcon(android.R.drawable.ic_menu_preferences);
		MenuItem itemClose = menu.add(0, 2, 4, "Exit");
		itemClose.setIcon(android.R.drawable.ic_menu_close_clear_cancel);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 0:
			return true;
		case 1:
			return true;
		case 2:
			finish();
			return true;
		default:
			return false;
		}
	}

	public void onChoice(Choice choice) {
		Station target = Questor.getCurrentGame().getCurrentQuest()
				.findStation(choice.getTargetId());
		goTo(target);
	}


	public void goTo(Station station) {
		String viewType;
		
		if (station == null)
			return;

		// Questor.getCurrentGame().setCurrentStation(station);

		viewType = "html";
		//viewType = "image";
		//viewType = "video";
		//viewType = "map";

		if (viewType == "html") {

			StringBuffer buf = new StringBuffer();
			for (Content c : station.getContents())
				if (checkCondition(c))
					buf.append(c.getQhtml());

			webView = new WebStoryView(this);
			setContentView(webView);
			webView.loadData(buf.toString(), "text/html", "UTF-8");

		} else if (viewType == "image") {
			
			ImageStoryView mImageView = new ImageStoryView(this);
			setContentView(mImageView);
			mImageView.setImageResource(R.drawable.logo);

		} else if (viewType == "video") {

			VideoStoryView mVideoView = new VideoStoryView(this);
			setContentView(mVideoView);
		    //mVideoView.playVideo(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.testmovie));
		    mVideoView.playVideo("/mnt/sdcard/questor/blubb.mp4");

		} else if (viewType == "map") {
			//MapView mMapView = new MapView(this);
			

		} else if (viewType == "compass") {

		} else if (viewType == "battle") {

		} else if (viewType == "dialog") {

		} else if (viewType == "finish") {

		}

	}

	private boolean checkCondition(Content c) {
		return true;
	}
}
